package com.manbu.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.manbu.springcloud.pojo.Dept;

import java.util.List;

/**
 * @description: 消费者不应该有service层 采用resultFUll 风格url 请求
 * @author: wanghan
 * @time: 2021/4/19 下午11:48
 */
@RestController
public class DeptConsumerDept{
    //在集群环境下我们不应该把一个服务地址写死 应该通过服务的名字方式去获取
    //private static  final String REST_URL_PREFIX="http://localhost:8001/";
    private static  final String REST_URL_PREFIX="http://SPRINGCLOUD-PROVIDER-DEPT";
    //RestTemplate 有很多方法供给我们调用 注册到spring
    @Autowired
    RestTemplate restTemplate;//提供多种便捷访问http远程服务的方法简单的Restfull服务模板

   /* @RequestMapping("")
    public boolean addDept(Dept dept){
        restTemplate.postForEntity()
    }*/
   @RequestMapping("/consumer/dept/get/{id}")
   public Dept addDep(@PathVariable("id") Long id){
       return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
   }

    @RequestMapping("/consumer/dept/add")
    public boolean addDep( Dept dept){
        Boolean aBoolean = restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", Dept.class, Boolean.class);
        return aBoolean;
    }
    @RequestMapping("/consumer/dept/list")
    public List<Dept> deptList(Dept dept){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);
    }

}
